<?php
declare (strict_types=1);

namespace addons\Builder\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

class CreateFolder extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('cf')
            ->setDescription('the CreateFolder command');
    }

    protected function execute(Input $input, Output $output)
    {
        try {
            //创建runtime
            $folder = app()->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR;
            if (!file_exists($folder)) {
                mkdir($folder);
                chmod($folder, 0777);
            }
            $autoCreateFolder = config('tool')['autoCreateFolder'];
            foreach ($autoCreateFolder as $folder) {
                $path = app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . $folder['name'];
                if (!file_exists($path)) {
                    mkdir($path, $folder['permissions']);
                    chmod($path, $folder['permissions']);
                }
            }
            $output->writeln('创建成功');
        } catch (\Exception $e) {
            $output->writeln('创建失败：' . $e->getMessage());
        }
    }
}
